package com.zhuozhou.jdyc.main.settlement.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;

import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.zhuozhou.jdyc.R;
import com.zhuozhou.jdyc.api.UserAccountHelper;
import com.zhuozhou.jdyc.base.BaseActivity;
import com.zhuozhou.jdyc.databinding.ActivityShopSettlementBinding;
import com.zhuozhou.jdyc.main.goods.bean.BasicsCommodityBase;
import com.zhuozhou.jdyc.main.main.bean.BasicsShoppingBean;
import com.zhuozhou.jdyc.main.main.viewmodel.MainViewModel;
import com.zhuozhou.jdyc.main.meinfo.activity.AddShippingAddressActivity;
import com.zhuozhou.jdyc.main.meinfo.activity.ShippingAddressActivity;
import com.zhuozhou.jdyc.main.meinfo.bean.ShippingAddressBean;
import com.zhuozhou.jdyc.main.settlement.adapter.SettlementShopAdapter;
import com.zhuozhou.jdyc.main.settlement.bean.PayBean;
import com.zhuozhou.jdyc.util.apiUtil.DateUtil;
import com.zhuozhou.jdyc.util.jiami.MD5Util;
import com.zhuozhou.jdyc.util.log.LogUtil;
import com.zhuozhou.jdyc.wight.recyclerview.MyLayoutManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Create by Once_ChuXy on 2021/8/25
 * Describe:结算
 */
public class ShopSettlementActivity extends BaseActivity<MainViewModel, ActivityShopSettlementBinding> {

    private SettlementShopAdapter settlementShopAdapter;
    public static int SHOPSETTLEMENT = 10086;
    IWXAPI msgApi = null;
    String[] strs = null;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_shop_settlement;
    }

    @Override
    public String setTitleBar() {
        return "提交订单";
    }

    @Override
    public void initView() {
        msgApi = WXAPIFactory.createWXAPI(context, null);
        msgApi.registerApp("wx745ff39c418ee1c0");//wxde4633c3c97927eb
        binding.tvAddLocation.setOnClickListener(v -> {
            //前往添加地址
            Intent in = new Intent(this, AddShippingAddressActivity.class);
            startActivity(in);
        });

        mViewModel.MySiteMutableLiveData.observe(this, v -> {
            if (v.getArray() != null && v.getArray().size() > 0)
                for (ShippingAddressBean.ArrayBean bean : v.getArray()) {
                    if (Integer.parseInt(bean.getStatus()) == 1) {
                        binding.setBean(bean);
                    }
                }
            else {
                binding.tvAddLocation.setVisibility(View.VISIBLE);
                binding.llLocation.setVisibility(View.GONE);
            }
        });
        mViewModel.DistributionMutableLiveData.observe(this, v -> {
            setShop((List<BasicsShoppingBean.ObjBeanX.ObjBean>) baseBundle.getSerializable("shop"), v);
        });
        binding.llLocation.setOnClickListener(v -> {
            Intent in = new Intent(this, ShippingAddressActivity.class);
            Bundle bundle = new Bundle();
            bundle.putBoolean("isSettlement", true);
            in.putExtras(bundle);
            startActivityForResult(in, SHOPSETTLEMENT);
        });
        binding.tvPay.setOnClickListener(v -> {
            //(Integer.parseInt(list.get(0).getType()) == 1 ? "" : Integer.parseInt(list.get(0).getType()) == 2 ? (strings[1] + "积分+") :
            //                    (strings[1] + "易货积分+")) + "￥" + strings[0]
            List<BasicsShoppingBean.ObjBeanX.ObjBean> beans = (List<BasicsShoppingBean.ObjBeanX.ObjBean>) baseBundle.getSerializable("shop");
            BasicsShoppingBean.ObjBeanX.ObjBean objBean = beans.get(0);
            PayBean pay = new PayBean();
            pay.setZongjia(strs[0]);
            pay.setType(objBean.getType());
            if (Integer.parseInt(objBean.getType()) == 2) {
                pay.setTotalIntegral(strs[1]);
            }
            pay.setSS_order(" ");
            pay.setOrderNo(" ");
            pay.setDianpuid(" ");
            pay.setX(" ");
            pay.setY(" ");
            PayBean.ListBeanX listBeanX = new PayBean.ListBeanX();
            listBeanX.setAdmin_id(objBean.getAdmin_id());
            listBeanX.setDiscount(objBean.getDiscount());
            listBeanX.setDiscountSelect(objBean.getDiscountSelect());
            listBeanX.setType(objBean.getType());
            List<PayBean.ListBeanX.ListBean> listBeans = new ArrayList<>();
            for (int i = 0; i < objBean.getShangpin().size(); i++) {
                PayBean.ListBeanX.ListBean bean = new PayBean.ListBeanX.ListBean();
                bean.setAdmin_id(objBean.getShangpin().get(i).getAdmin_id());
                bean.setCommpdity_id(objBean.getShangpin().get(i).getId());
                bean.setGoods_no(objBean.getShangpin().get(i).getNumber() + "");
                bean.setGoods_price(objBean.getShangpin().get(i).getGoods_price());
                bean.setGoods_weight(objBean.getShangpin().get(i).getGoods_weight());
                bean.setGoodsname(objBean.getShangpin().get(i).getName());
                bean.setHuoyun(objBean.getShangpin().get(i).getHuoyun());
                bean.setName(binding.getBean().getName());
                bean.setPhone(binding.getBean().getPhone());
                bean.setSite(binding.getBean().getCity_details());
                bean.setNumber(objBean.getShangpin().get(i).getNumber() + "");
                bean.setNumberlist(objBean.getShangpin().get(i).getNumberlist());
                bean.setPostage("0.00");
                bean.setPostage_single(i);
                bean.setQishou_price(objBean.getShangpin().get(i).getQishou_price());
                bean.setShoppingid(objBean.getShangpin().get(i).getShoppingid());
                bean.setSku(objBean.getShangpin().get(i).getSku());
                bean.setSkustring(objBean.getShangpin().get(i).getSkustring());
                bean.setSpec_image(objBean.getShangpin().get(i).getSpec_image());
                bean.setUnit(objBean.getShangpin().get(i).getUnit());
                bean.setWuliu(objBean.getShangpin().get(i).getWuliu());
                listBeans.add(bean);
            }
            listBeanX.setList(listBeans);
            listBeanX.setNickname(objBean.getNickname());
            listBeanX.setPrice(strs[0]);
            listBeanX.setPrice2(strs[0]);
            listBeanX.setShop_postage("0.00");
            listBeanX.setSS_order(" ");
            listBeanX.setUser_id(UserAccountHelper.getUserID());
            List<PayBean.ListBeanX> list = new ArrayList<>();
            list.add(listBeanX);
            pay.setTrade_type("APP");
            pay.setList(list);
            mViewModel.toPay(pay, UserAccountHelper.getUserID());
        });
        mViewModel.BasicsPayMutableLiveData.observe(this, v -> {
//            IWXAPI api;
            PayReq request = new PayReq();
            request.appId = v.getAppId();
            request.partnerId = "16011999986";
            String[] strarray = v.getPackageX().split("=");
            request.prepayId = strarray[1];
            request.packageValue = "Sign=WXPay";
            request.nonceStr = v.getNonceStr();
            request.timeStamp = v.getTimeStamp();
            request.sign = v.getPaySign();
            LogUtil.show(request.toString());
            msgApi.sendReq(request);
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        mViewModel.getMySite(UserAccountHelper.getUserID() + "");
    }

    @Override
    public void initData(Bundle bundle) {
        mViewModel.getMySite(UserAccountHelper.getUserID() + "");
        mViewModel.getDistribution(((List<BasicsShoppingBean.ObjBeanX.ObjBean>) baseBundle.getSerializable("shop")).get(0).getAdmin_id() + "");
    }

    private void setShop(List<BasicsShoppingBean.ObjBeanX.ObjBean> list, List<BasicsCommodityBase.DataBean.DistributionBean> DistributionList) {
        list.get(0).setDistributionList(DistributionList);
        settlementShopAdapter = new SettlementShopAdapter(this);
        settlementShopAdapter.setList(list);
        settlementShopAdapter.setOnItemSetMoneyListener(strings -> {
            binding.tvMoney.setText((Integer.parseInt(list.get(0).getType()) == 1 ? "" : Integer.parseInt(list.get(0).getType()) == 2 ? (strings[1] + "积分+") :
                    (strings[1] + "易货积分+")) + "￥" + strings[0]);
            strs = strings;
        });
        binding.mRecyclerViewShop.setAdapter(settlementShopAdapter);
        binding.mRecyclerViewShop.setLayoutManager(new MyLayoutManager(this));
//        String[] strings = getAllMoney(list);
//        binding.tvMoney.setText((Integer.parseInt(list.get(0).getType()) == 1 ? "" : Integer.parseInt(list.get(0).getType()) == 2 ? (strings[1] + "积分+") :
//                (strings[1] + "易货积分+")) + "￥" + strings[0]);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == SHOPSETTLEMENT && resultCode == SHOPSETTLEMENT) {
            binding.setBean((ShippingAddressBean.ArrayBean) data.getSerializableExtra("data"));
        }
    }
}
